<?php

namespace addons\TfFutures\backend\controllers;

use addons\TfFutures\common\models\Lever;
use addons\TfFutures\common\models\SymbolCate;
use common\helpers\ArrayHelper;
use yii;
use common\components\Curd;
use addons\TfFutures\common\models\Symbol;
use yii\data\Pagination;

class SymbolController extends BaseController
{
    use Curd;
    public $modelClass = Symbol::class;

    public function actionIndex()
    {
        $data   = $this->modelClass::find();

        $type = Yii::$app->request->get('type');
        $name = Yii::$app->request->get('name');
        $code = Yii::$app->request->get('code');

        if (!empty($type)) {
            $data->andWhere(['cate_id'=>$type]);
        }
        if (!empty($name)) {
            $data->andWhere(['like','name',$name]);
        }
        if (!empty($code)) {
            $data->andWhere(['code2'=>$code]);
        }

        $pages  = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
        $models = $data->offset($pages->offset)
                       ->orderBy('id desc')
                       ->limit($pages->limit)
                       ->all();
//        $cateDropDownList = SymbolCate::getEditDropDownList();unset($cateDropDownList[0]);

        $cateDropDownList = SymbolCate::find()->select('id,title')->where(['status'=>1])->asArray()->all();
        $cateDropDownList=ArrayHelper::map($cateDropDownList,"id","title");

        return $this->render($this->action->id, [
            'models' => $models,
            'pages'  => $pages,
            'cateDropDownList' => $cateDropDownList,
            'type' => $type,
            'name' => $name,
            'code' => $code,
        ]);
    }

    public function actionRisk(){
        $data   = $this->modelClass::find();
        $pages  = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
        $type = Yii::$app->request->get('type');
        $name = Yii::$app->request->get('name');
        $code = Yii::$app->request->get('code');

        if (!empty($type)) {
            $data->andWhere(['cate_id'=>$type]);
        }
        if (!empty($name)) {
            $data->andWhere(['name'=>$name]);
        }
        if (!empty($code)) {
            $data->andWhere(['code2'=>$code]);
        }
        $models = $data->offset($pages->offset)
            ->orderBy('id desc')
            ->limit($pages->limit)
            ->all();
        $cateDropDownList = SymbolCate::getEditDropDownList($id);unset($cateDropDownList[0]);
        return $this->render($this->action->id, [
            'models' => $models,
            'pages'  => $pages,
            'cateDropDownList' => $cateDropDownList,
            'type' => $type,
            'name' => $name,
            'code' => $code,
        ]);
    }

    /*
     * 开启/关闭自动风控
     * */
    public function actionRiskAuto($id){
        $model = Symbol::findOne($id);
        $model->risk_auto = $model->risk_auto==0 ? 1 : 0;
        if ($model->save()) {
            $this->message("修改成功", $this->redirect(['risk']), 'success');
        } else {
            $this->message($this->getError($model), $this->redirect(['risk']), 'error');
        }
    }



    /*
     * 设置滑点
     * */
    public function actionSetRisk()
    {
        $id = Yii::$app->request->get('id');
        $model = Symbol::findOne($id);

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                $this->message("修改成功", $this->redirect(['risk']), 'success');
            } else {
                $this->message($this->getError($model), $this->redirect(['risk']), 'error');
            }

        }

        return $this->renderAjax('set-risk', ['model' => $model]);
    }

    /**
     * 返回模型
     *
     * @param $id
     * @return \yii\db\ActiveRecord
     */
    protected function findModel($id)
    {
        /* @var $model \yii\db\ActiveRecord */
        if (empty($id) || empty(($model = $this->modelClass::find()->where(['id' => $id])->one()))) {
            $model = new $this->modelClass;
            return $model->loadDefaultValues();
        }

        return $model;
    }

    /**
     * @return mixed|string|\yii\console\Response|\yii\web\Response
     * @throws \yii\base\ExitException
     */
    public function actionAjaxEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id');
        $model = $this->findModel($id);
        // ajax 验证
        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {
//            $model->lever_id = implode(',',$model->lever_id);

		        
		        	return $model->save()
                ? $this->message('成功', $this->redirect(['index']), 'success')
                : $this->message($this->getError($model), $this->redirect(['index']), 'error');
		        
	            
        }
        //查询可用杠杆
//        $lever = Lever::find()->select('id,name')->asArray()->all();
//
//        $lever = ArrayHelper::map($lever, 'id', 'name');
//        $model->lever_id = explode(',',$model->lever_id);

        $cateDropDownList = SymbolCate::find()->select('id,title')->where(['status'=>1])->asArray()->all();
        $cateDropDownList=ArrayHelper::map($cateDropDownList,"id","title");


        return $this->renderAjax($this->action->id, [
            'model' => $model,
//            'lever' => $lever,
            // 'isadd' => $isadd,
            'cateDropDownList' => $cateDropDownList,
        ]);
    }
    public function actionEdit($id)
    {
        $model = $this->modelClass::findOne($id);
        $cateDropDownList = SymbolCate::getEditDropDownList($id);unset($cateDropDownList[0]);
        return $this->render($this->action->id, [
            'model' => $model,
            'cateDropDownList' => $cateDropDownList,
        ]);
    }

    /**
     * 删除
     *
     * @param $id
     * @return mixed
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelete($id)
    {
        if ($this->modelClass::findOne($id)->delete()) {
            return $this->message("删除成功", $this->redirect(['index']));
        }

        return $this->message("删除失败", $this->redirect(['index']), 'error');
    }

}